using System;
using System.Linq;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0009. 回文数")]
    public class No0009_IsPalindrome : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(IsPalindrome, -121, false);
            Assert.TestExe(IsPalindrome, 0, true);
            Assert.TestExe(IsPalindrome, 10, false);
            Assert.TestExe(IsPalindrome, 121, true);
            Assert.TestExe(IsPalindrome, 123321, true);
            Assert.TestExe(IsPalindrome, 123421, false);
            Assert.TestExe(IsPalindrome, 12421, true);
            Assert.TestExe(IsPalindrome, 12431, false);
        }

        public bool IsPalindrome(int x)
        {
            if (x < 0 || (x % 10 == 0 && x != 0))
            {
                return false;
            }

            int rnum = 0;
            while (x > rnum)
            {
                rnum = rnum * 10 + x % 10;
                x /= 10;
            }
            return x == rnum || x == rnum / 10;
        }
    }
}
